package ru.ssau.university.ejb.impl;

import java.util.List;

import javax.ejb.Stateless;

import ru.ssau.university.ejb.api.UniversityServiceLocal;
import ru.ssau.university.ejb.api.UniversityServiceRemote;
import ru.ssau.university.persistence.dao.AdminDAO;
import ru.ssau.university.persistence.dao.DepartmentDAO;
import ru.ssau.university.persistence.dao.FacultyDAO;
import ru.ssau.university.persistence.dao.GroupDAO;
import ru.ssau.university.persistence.dao.PlanItemDAO;
import ru.ssau.university.persistence.dao.ProgressDAO;
import ru.ssau.university.persistence.dao.ScheduleDAO;
import ru.ssau.university.persistence.dao.StudentDAO;
import ru.ssau.university.persistence.dao.TeacherDAO;
import ru.ssau.university.persistence.entity.Admin;
import ru.ssau.university.persistence.entity.Department;
import ru.ssau.university.persistence.entity.Faculty;
import ru.ssau.university.persistence.entity.Group;
import ru.ssau.university.persistence.entity.PlanItem;
import ru.ssau.university.persistence.entity.Progress;
import ru.ssau.university.persistence.entity.Schedule;
import ru.ssau.university.persistence.entity.Student;
import ru.ssau.university.persistence.entity.Teacher;

@Stateless
public class UniversityServiceImpl implements UniversityServiceLocal, UniversityServiceRemote {
	
	private FacultyDAO    facultyDAO;
	private DepartmentDAO departmentDAO;
	private TeacherDAO    teacherDAO;
	private AdminDAO      adminDAO;
	private GroupDAO      groupDAO;
	private StudentDAO    studentDAO;
	private ScheduleDAO   scheduleDAO;
	private PlanItemDAO   planItemDAO;
	private ProgressDAO   progressDAO;
	
	public UniversityServiceImpl() {
		facultyDAO    = new FacultyDAO();
		departmentDAO = new DepartmentDAO();
		teacherDAO    = new TeacherDAO();
		adminDAO      = new AdminDAO();
		groupDAO      = new GroupDAO();
		studentDAO    = new StudentDAO();
		scheduleDAO   = new ScheduleDAO();
		planItemDAO   = new PlanItemDAO();
		progressDAO   = new ProgressDAO();
	}

	@Override
	public List<Faculty> getFaculties() {
		return facultyDAO.getAll();
	}

	@Override
	public List<Department> getDepartments() {
		return departmentDAO.getAll();
	}
	
	public List<Department> getDepartmentsForFaculty(Long facultyId) {
		return departmentDAO.getDepartmentsForFaculty(facultyId);
	}

	@Override
	public Admin loginAsAdmin(String userName, String password) {
		return adminDAO.getAdminBy(userName, password);
	}

	@Override
	public List<Teacher> getTeacherByDepartment(Long departmentId) {
		return teacherDAO.getTeachersByDepartment(departmentId);
	}

	@Override
	public Teacher loginAsTeacher(Long teacherId, String password) {
		return teacherDAO.getTeacherByIdAndPassword(teacherId, password);
	}

	@Override
	public List<Group> getGroupsByFacultyId(Long id) {
		return groupDAO.getGroupsByFacultyId(id);
	}

	@Override
	public List<Student> getStudentsByGroup(Long id) {
		return studentDAO.getStudentsByGroup(id);
	}

	@Override
	public void saveOrUpdateStudent(Student s) {
		if (s.getFullName() != null && s.getFullName().length() > 0) {
			if (s.getId() == null) {
				studentDAO.save(s);
			} else {
				studentDAO.updateInBase(s);
			}
		}
	}

	@Override
	public Schedule getSchedule(Long groupId, int week, int dayOfWeek, int lesson) {
		return scheduleDAO.getSchedule(groupId, week, dayOfWeek, lesson);
	}

	@Override
	public Group getGroupById(Long groupId) {
		return groupDAO.findById(groupId);
	}

	@Override
	public List<PlanItem> getPlanItems(Long specId, int term) {
		return planItemDAO.getPlanItems(specId, term);
	}

	@Override
	public void saveOrUpdateSchedule(Schedule schedule) {
		if (schedule.getId() == null) {
			scheduleDAO.save(schedule);
		} else {
			scheduleDAO.updateInBase(schedule);
		}
	}

	@Override
	public void deleteSchedule(Schedule schedule) {
		scheduleDAO.delete(schedule);
	}

	@Override
	public Progress getProgress(Long studentId, Long planItemId, int numb) {
		return progressDAO.getProgress(studentId, planItemId, numb);
	}

	@Override
	public void saveOrUpdateProgress(Progress p) {
		if (p.getId() == null) {
			progressDAO.save(p);
		} else {
			progressDAO.updateInBase(p);
		}
	}

	@Override
	public void deleteStudent(Long studentId) {
		Student student = studentDAO.findById(studentId);
		studentDAO.delete(student);
	}

}
